﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace YJY_DMZS.Barcode
{
    public partial class FrmBarcodeTrace : XtraForm
    {
        public FrmBarcodeTrace()
        {
            InitializeComponent();
            InitGrid();
        }



        private void FrmBarcodeTrace_Load(object sender, EventArgs e)
        {
            DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            datBegin.DateTime = dt.Date;
            datEnd.DateTime = DateTime.Now.Date;
            cbbStatus.SelectedIndex = 0;
        }

        //查询
        private void btnSearch_Click(object sender, EventArgs e)
        {
            DateTime dtBegin = datBegin.DateTime.Date;
            DateTime dtEnd = datEnd.DateTime.Date;
            string barocde_begin = txtBarcode_begin.Text.Trim();
            string barcode_end = txtBarcode_end.Text.Trim();
            string barcodeStatus = cbbStatus.Text;
            
            string productCode = txtProductCode.Text.Trim();
            string number_rkd = txtNumber_rkd.Text.Trim();
            string number_ckd = txtNumber_ckd.Text.Trim();

            DMZS.BLL.Barcode bll = new DMZS.BLL.Barcode();
            DataSet ds = bll.GetBarocdeTarce(dtBegin,dtEnd,barocde_begin,barcode_end, barcodeStatus, productCode, number_rkd, number_ckd);
            grdBody.DataSource = ds.Tables[0];
        }

        private void grvBody_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "Status")
            {
                switch (Convert.ToInt32(e.Value))
                {
                    case 0: e.DisplayText = "未打印"; break;
                    case 1: e.DisplayText = "已打印"; break;
                    case 10: e.DisplayText = "入库待审"; break;
                    case 11: e.DisplayText = "入库已审"; break;
                    case 20: e.DisplayText = "出库待审"; break;
                    case 21: e.DisplayText = "出库已审"; break;
                    case 30: e.DisplayText = "退货待审"; break;
                    case 31: e.DisplayText = "退货已审"; break;
                    default: e.DisplayText = "未定义状态"; break;
                }
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (grvBody.RowCount == 0) return;

            SaveFileDialog dlg = new SaveFileDialog();
            string name =
            dlg.Title = "导出到Excel";
            dlg.FileName = "条码信息文件";
            dlg.Filter = "Microsoft Excel|*.xls";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                grdBody.ExportToXls(dlg.FileName);
            } 
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (grvBody.RowCount == 0) return;
            DataTable dt = grdBody.DataSource as DataTable;
            if (dt.Rows.Count == 0) return;

            if (System.IO.File.Exists(Application.StartupPath + "\\frxFiles\\" + "产品条码模板.frx"))
            {
                FastReport.Report rpt = new FastReport.Report();
                rpt.Load(Application.StartupPath + "\\frxFiles\\" + "产品条码模板.frx");
                DataTable dtnew = dt.Copy();
                dtnew.TableName = "条码信息";
                dtnew.Columns["Code"].ColumnName = "条码";
                dtnew.Columns["ProductCode"].ColumnName = "产品编号";
                dtnew.Columns["ProductName"].ColumnName = "产品名称";
                dtnew.Columns["Specification"].ColumnName = "规格";
                dtnew.Columns["Model"].ColumnName = "型号";
                rpt.RegisterData(dtnew, "条码信息");
                rpt.Show(true);
                if (DialogResult.Yes == XtraMessageBox.Show("是否已打印？", "打印记录", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    UpdatePrintCount(dt);
                }
            }
            else
            {
                XtraMessageBox.Show("未发现打印文件\"rptFiles\"产品条码模板.frx。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        /// <summary>
        /// 更新打印次数（次数非张数）
        /// </summary>
        private void UpdatePrintCount(DataTable dt)
        {
            DMZS.BLL.Barcode bll = new DMZS.BLL.Barcode();
            foreach (DataRow row in dt.Rows)
            {
                bll.UpdatePrintCount(row["Code"].ToString());
            }
        }

        /// <summary>
        /// 初始网格显示
        /// </summary>
        private void InitGrid()
        {
            grvBody.IndicatorWidth = grvBody.IndicatorWidth == -1 ? 40 : grvBody.IndicatorWidth;
            grvBody.OptionsView.ShowIndicator = true;

            CommonControl.DxGridHelper.InitGridStyle(grvBody);
        }

        private void grvBody_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            CommonControl.DxGridHelper.DrawRowIndicator(sender, e);
        }
    }
}
